#include <gtk/gtk.h>
#include <atk/atk.h>

// 打印ATK对象信息的辅助函数
static void print_atk_info(GtkWidget *widget, const gchar *widget_name) {
    // 获取Widget对应的ATK对象
    AtkObject *atk_obj = gtk_widget_get_accessible(widget);
    if (!atk_obj) {
        g_print("%s 没有对应的ATK对象\n", widget_name);
        return;
    }

    // 打印ATK对象信息
    const gchar *atk_type = G_OBJECT_TYPE_NAME(atk_obj);
    g_print("%s 对应的ATK对象类型: %s\n", widget_name, atk_type);

    // 检查实现的接口
    if (ATK_IS_ACTION(atk_obj)) {
        g_print("%s 实现了AtkAction接口\n", widget_name);
    }
    if (ATK_IS_COMPONENT(atk_obj)) {
        g_print("%s 实现了AtkComponent接口\n", widget_name);
    }
    if (ATK_IS_TEXT(atk_obj)) {
        g_print("%s 实现了AtkText接口\n", widget_name);
    }
}

// 按钮点击回调函数
static void on_button_clicked(GtkWidget *widget, gpointer data) {
    (void)widget;
    GtkEntry *entry = GTK_ENTRY(data);
    const gchar *text = gtk_entry_get_text(entry);
    g_print("输入框内容: %s\n", text);

    // 获取输入框的ATK文本接口并打印内容
    AtkObject *atk_obj = gtk_widget_get_accessible(GTK_WIDGET(entry));
    if (ATK_IS_TEXT(atk_obj)) {
        AtkText *atk_text = ATK_TEXT(atk_obj);
        gchar *atk_text_content = atk_text_get_text(atk_text, 0, -1);
        g_print("通过ATK获取的文本内容: %s\n", atk_text_content);
        g_free(atk_text_content);
    }
}


int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);

    // 创建主窗口
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "无障碍示例程序");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

    // 创建垂直布局容器
    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    // 创建标签（新增部分）
    GtkWidget *label = gtk_label_new("测试ATK与ATSPI联系");
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); // 将标签添加到垂直容器顶部

    // 创建文本输入框
    GtkWidget *entry = gtk_entry_new();
    gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "请输入内容");
    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);

    // 创建按钮
    GtkWidget *button = gtk_button_new_with_label("提交");
    g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), entry);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);

    // 显示所有元素
    gtk_widget_show_all(window);

    // 打印各个Widget的ATK对象信息
    print_atk_info(window, "窗口");
    print_atk_info(vbox, "垂直容器");
    print_atk_info(label, "标签");  
    print_atk_info(entry, "输入框");
    print_atk_info(button, "按钮");

    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_main();
    return 0;
}